/**
 * Created by CL on 2017/4/26.
 */
if(sessionStorage.getItem('user')){
    $(".icon-person").html(sessionStorage.getItem('phone'));
    getHmcodes();
}
else{
    location.href="index.html";
}

$(".code-button").click(function () {
    location.href="createcode.html"
});
// $(".out-button").click(function () {
// });
$(".change-button").click(function () {
    $("#iosDialog2").fadeToggle(100);
});
$(".sure-logout").click(function () {
    $("#iosDialog2").fadeToggle(100);
    clearCookie('token');
    sessionStorage.removeItem('user');
    location.href="index.html?action=login"
});
$(".cancel-logout").click(function () {
    $("#iosDialog2").fadeToggle(100);
});
$(".icon-back").click(function () {
    location.href="index.html"
    }
);
function getHmcodes() {
    $.ajax(
        {
            type: "post",
            url: top_url+"hmcode/getHmCodes",
            dataType: "json",
            data: {
                token: sessionStorage.getItem('user')
            },
            success: function (data) {
                console.log(data);
                if (data.status == '200') {
                    Hmcodesreport(data);
                }
                if (data.status == '400') {

                }
            }
        }
    )
}

function parseURL(url) {
    var a =  document.createElement('a');
    a.href = url;
    return {
        source: url,
        protocol: a.protocol.replace(':',''),
        host: a.hostname,
        port: a.port,
        query: a.search,
        params: (function(){
            var ret = {},
                seg = a.search.replace(/^\?/,'').split('&'),
                len = seg.length, i = 0, s;
            for (;i<len;i++) {
                if (!seg[i]) { continue; }
                s = seg[i].split('=');
                ret[s[0]] = s[1];
            }
            return ret;
        })(),
        file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
        hash: a.hash.replace('#',''),
        path: a.pathname.replace(/^([^\/])/,'/$1'),
        relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
        segments: a.pathname.replace(/^\//,'').split('/')
    };
}

function getWebRoot() {
    var urlParam = top.parseURL(top.location.href);
    var web_root = urlParam.protocol + '://' + urlParam.host;

    if(!(urlParam.protocol == 'http' && (urlParam.port == 80 ||!urlParam.port)) &&
        !(urlParam.protocol =='https' && (urlParam.port == 443 ||!urlParam.port)))
    {
        web_root = web_root + ':' + urlParam.port + '';
    }
    return web_root;
}

function Hmcodesreport(data) {
    var report=data.payload;
    var html;
    var url;
    $.each(report,function (index) {
        // console.log(report[index])
            html="<li><p class='create-ul-title'>"
            +report[index].hmName+
            "</p><p class='create-main'><span class='dis-cell qrcode' id='qrcode"+index+"'>" +
                "</span><span class='dis-cell content2'>图片：<span>"
            +report[index].count+
            "张</span><br>阀值：<span>"
            +report[index].threshold+
            "次</span></span><span class='dis-cell content3'>扫描总数<br><span>"
            +report[index].totalNumber+
            "次</span></span></p><p class='create-button'><span class='edit' hid='"+index+"'>编辑</span><span class='code-delete' hid='"+report[index].hid+"'>删除</span></p></li>"
        $(".create-ul ul").append(html);
        url=getWebRoot()+"/hm/"+sessionStorage.getItem('uid')+"_"+report[index].hid;
        var qrcode = new QRCode(document.getElementById("qrcode"+index), {
            width : 120,//设置宽高
            height : 120
        });
        qrcode.makeCode(url);
    });
    $(".edit").on('click',function(){
            var id=String($(this).attr('hid'));
            sessionStorage.setItem('hid',report[id].hid);
            sessionStorage.setItem('hmInfo',JSON.stringify(report[id]));
            location.href="editcode.html";
            var url=String($(this).parent('p').siblings('.create-main').find('.qrcode').attr('title'));
            sessionStorage.setItem('codeurl',url);
        }
    );
    var id;
    $(".code-delete").click(function () {
        $("#iosDialog1").fadeToggle(100);
        id=String($(this).attr('hid'));
    });
    $(".sure-delete").click(function () {
        $("#iosDialog1").fadeToggle(100);
        $("#loadingToast").fadeToggle(250);
        setTimeout(function () {
            delHmCodes(id);
        },1000)
    });
    $(".cancel-delete").click(function () {
        $("#iosDialog1").fadeToggle(100);
    });
}
function delHmCodes(id) {
    $.ajax({
        type: "post",
        url: top_url+"hmcode/delHmCodes",
        dataType: "json",
        data: {
            token: sessionStorage.getItem('user'),
            hid:id
        },
        success: function (data) {
            console.log(data);
            if (data.status == '200') {
                $("#loadingToast").fadeToggle(250);
                $(".success").html("删除成功");
                $("#toast").fadeToggle(250);
                setTimeout(toasttoggle, 700);
                setTimeout(function () {
                    location.href="codemain.html"
                }, 1000);
            }
            else{
                $("#loadingToast").fadeToggle(250);
                $(".failed").html(data.err);
                $("#failtoast").fadeToggle(250);
                setTimeout(failtoggle, 700);
            }
        }
        }
    )
}